package com.atguigu.spzx.service.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.spzx.service.product.mapper.ProductMapper;
import com.atguigu.spzx.service.product.service.ProductDetailsService;
import com.atguigu.spzx.service.product.service.ProductService;
import com.atguigu.spzx.service.product.service.ProductSkuService;
import com.atguigu.spzx.model.dto.h5.ProductSearchDto;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.vo.h5.ProductDetailsVo;
import com.atguigu.spzx.model.vo.h5.SpecValueVo;
import com.atguigu.spzx.model.vo.product.ProductVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * title:
 * author: zgl
 * date: 2024/1/15
 * description：
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Resource
    private ProductSkuService productSkuService;

    @Resource
    private ProductDetailsService productDetailsService;

    @Override
    public PageInfo<ProductSku> getProductListByPage(Integer page, Integer limit, ProductSearchDto searchDto) {
        Page<ProductSku> pageCondition = new Page<>(page, limit);
        QueryWrapper<ProductSku> wrapper = Wrappers.query();
        String keyword = searchDto.getKeyword();
        Integer category1Id = searchDto.getCategory1Id();
        Integer category2Id = searchDto.getCategory2Id();
        Integer category3Id = searchDto.getCategory3Id();
        Integer brandId = searchDto.getBrandId();
        Integer order = searchDto.getOrder();

        if (keyword != null) {
            wrapper.like("t1.sku_name", keyword);
        }
        if (category1Id != null) {
            wrapper.eq("t2.category1_id", category1Id);
        }
        if (category2Id != null) {
            wrapper.eq("t2.category2_id", category2Id);
        }
        if (category3Id != null) {
            wrapper.eq("t2.category3_id", category3Id);
        }
        if (brandId != null) {
            wrapper.eq("t2.brand_id", brandId);
        }
        if (order != null) {
            if (order == 1) {
                wrapper.orderByDesc("t1.sale_num");
            }
            if (order == 2) {
                wrapper.orderByAsc("t1.sale_price");
            }
            if (order == 3) {
                wrapper.orderByDesc("t1.sale_price");
            }
        }
        List<ProductSku> productSkuList = productMapper.selectByPage(pageCondition, wrapper);
        pageCondition.setRecords(productSkuList);
        PageInfo<ProductSku> pageInfo = new PageInfo<>(productSkuList);
        pageInfo.setPages((int) pageCondition.getPages());
        pageInfo.setPageNum((int) pageCondition.getCurrent());
        pageInfo.setPageSize((int) pageCondition.getSize());
        pageInfo.setTotal(pageCondition.getTotal());
        return pageInfo;
    }


    @Cacheable(value = "spzx:product:details:cache",key = "#skuId")
    @Override
    public ProductDetailsVo findBrandDetails(Long skuId) {
        ProductDetailsVo productDetailsVo = new ProductDetailsVo();

        ProductSku productSku = productSkuService.getById(skuId);
        Long productId = productSku.getProductId();
        productDetailsVo.setProductSku(productSku);

        Product product = this.getById(productId);
        ProductVo productVo = new ProductVo();
        BeanUtils.copyProperties(product, productVo);
        //分类名称和品牌名称(等待补充)
        productDetailsVo.setProduct(productVo);

        //详情图
        String[] imageUrls = productDetailsService.getById(productId).getImageUrls().split(",");
        List<String> imageUrlsList = Arrays.asList(imageUrls);
        productDetailsVo.setDetailsImageUrlList(imageUrlsList);

        //规格信息
        //[{"key":"颜色","valueList":["白色"]},{"key":"内存","valueList":["8G","12G"]}]
        String specValue = product.getSpecValue();
        List<SpecValueVo> specValueVos = JSON.parseArray(specValue, SpecValueVo.class);

        productDetailsVo.setSpecValueList(specValueVos);

        //skuId 和 specValue对应的关系
        Map<String, Long> skuSpecValueMap = productSkuService.list(Wrappers.lambdaQuery(ProductSku.class).eq(ProductSku::getProductId, productId)
                .select(ProductSku::getId, ProductSku::getSkuSpec)).stream().collect(Collectors.toMap(ProductSku::getSkuSpec, ProductSku::getId));
        productDetailsVo.setSkuSpecValueMap(skuSpecValueMap);

        //轮播图
        String[] sliderUrls = product.getSliderUrls().split(",");
        List<String> sliderUrlsList = Arrays.asList(sliderUrls);
        productDetailsVo.setSliderUrlList(sliderUrlsList);

        return productDetailsVo;
    }
}
